package service;

import base.InitExcel;
import base.InitImageAndFont;
import com.sun.image.codec.jpeg.ImageFormatException;
import config.FontConfig;
import config.LocationConfig;
import model.User;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Calendar;

public class ImageService implements Runnable {
    Font font50 = FontConfig.getQmFont(50.0F);
    Font font70 = FontConfig.getQmFont(70.0F);

    public ImageService() {
    }

    public void run() {
        while(true) {
            try {
                User user = InitExcel.getOneUser();
                if (user != null) {
                    System.out.println("正在生成：" + user.getName() + ":编号" + user.getNo());
                    Image src = ImageIO.read(InitImageAndFont.baseFile);
                    int wideth = src.getWidth((ImageObserver)null);
                    int height = src.getHeight((ImageObserver)null);
                    BufferedImage image = new BufferedImage(wideth, height, 1);
                    Graphics2D g = image.createGraphics();
                    g.drawImage(src, 0, 0, wideth, height, (ImageObserver)null);
                    Image src_biao = ImageIO.read(InitImageAndFont.zzFile);
                    int wideth_biao = src_biao.getWidth((ImageObserver)null);
                    int height_biao = src_biao.getHeight((ImageObserver)null);
                    g.setComposite(AlphaComposite.getInstance(10, 0.4F));
                    g.drawImage(src_biao, LocationConfig.ZZ.getX().intValue(), LocationConfig.ZZ.getY().intValue(), wideth_biao, height_biao, (ImageObserver)null);
                    g.setComposite(AlphaComposite.getInstance(3));
                    g.setColor(Color.black);
                    g.setFont(this.font50);
                    g.drawString(user.getNo().toString(), LocationConfig.NO.getX().intValue(), LocationConfig.NO.getY().intValue());
                    g.drawString(user.getName().toString(), LocationConfig.NAME.getX().intValue(), LocationConfig.NAME.getY().intValue());
                    g.drawString(user.getMobile(), LocationConfig.MOBILE.getX().intValue(), LocationConfig.MOBILE.getY().intValue());
                    Calendar c = Calendar.getInstance();
                    c.setTime(user.getTime());
                    g.drawString(c.get(Calendar.YEAR)+"", LocationConfig.Y.getX().intValue(), LocationConfig.Y.getY().intValue());
                    g.drawString(c.get(Calendar.MONTH)+"", LocationConfig.M.getX().intValue(), LocationConfig.M.getY().intValue());
                    g.drawString(c.get(Calendar.DAY_OF_MONTH)+"", LocationConfig.D.getX().intValue(), LocationConfig.D.getY().intValue());
                    g.drawString(c.get(Calendar.YEAR)+"", LocationConfig.ZY.getX().intValue(), LocationConfig.Y.getY().intValue());
                    g.drawString(c.get(Calendar.MONTH)+"", LocationConfig.ZM.getX().intValue(), LocationConfig.M.getY().intValue());
                    g.drawString(c.get(Calendar.DAY_OF_MONTH)+"", LocationConfig.ZD.getX().intValue(), LocationConfig.D.getY().intValue());
                    g.setFont(this.font70);
                    g.drawString(user.getName(), LocationConfig.QM.getX().intValue(), LocationConfig.QM.getY().intValue());
                    g.dispose();
                    ImageIO.write(image, "JPEG", new File("export/" + user.getNo() + ".jpg"));
                    continue;
                }
            } catch (FileNotFoundException var10) {
                var10.printStackTrace();
            } catch (ImageFormatException var11) {
                var11.printStackTrace();
            } catch (IOException var12) {
                var12.printStackTrace();
            }

            return;
        }
    }
}
